
package io.github.maojian.arr;

import org.junit.Test;

/**
 * @Auther: jian.mao
 * @Date: 2022/4/12 11:19
 * @Description: https://leetcode-cn.com/problems/find-the-middle-index-in-array/
 */
public class Q1991 {
    public int pivotIndex(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        int leftSum = 0;
        for (int i = 0; i < nums.length; i++) {
            int curNum = nums[i];
            if ((sum - curNum) == leftSum) {
                return i;
            }
            sum -= curNum;
            leftSum += curNum;
        }
        return -1;
    }

    @Test
    public void test() {
        int[] nums = new int[]{2, 3, -1, 8, 4};
        System.out.println(pivotIndex(nums));
    }
}
